package cn.chuan.keygen.module.verify.service.card;

import cn.chuan.keygen.framework.common.pojo.PageResult;
import cn.chuan.keygen.framework.common.util.object.BeanUtils;
import cn.chuan.keygen.framework.security.core.util.SecurityFrameworkUtils;
import cn.chuan.keygen.module.verify.controller.admin.card.vo.CardPageReqVO;
import cn.chuan.keygen.module.verify.controller.admin.card.vo.CardSaveReqVO;
import cn.chuan.keygen.module.verify.controller.app.card.vo.CardUpdateVO;
import cn.chuan.keygen.module.verify.dal.dataobject.card.CardDO;
import cn.chuan.keygen.module.verify.dal.mysql.card.CardMapper;
import cn.chuan.keygen.module.verify.enums.CardTypeEnum;
import cn.chuan.keygen.module.verify.enums.ClosureEnum;
import cn.chuan.keygen.module.verify.enums.SellStatusEnum;
import cn.chuan.keygen.module.verify.enums.UseStatusEnum;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Date;

import static cn.chuan.keygen.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.chuan.keygen.module.verify.enums.ErrorCodeConstants.*;

/**
 * 充值卡 Service 实现类
 *
 * @author chuan
 */
@Service
@Validated
public class CardServiceImpl implements CardService {

    @Resource
    private CardMapper cardMapper;

    @Override
    public Long createCard(CardSaveReqVO createReqVO) {
        //设置状态
        createReqVO.setSellStatus(SellStatusEnum.NOT_SOLD.getStatus());
        createReqVO.setUseStatus(UseStatusEnum.NOT_USED.getStatus());
        createReqVO.setClosure(ClosureEnum.UN_CLOSURE.getStatus());
        // 插入
        CardDO card = BeanUtils.toBean(createReqVO, CardDO.class);
        //设置卡号
        card.setCardNumber(RandomUtil.randomNumbers(16));
        //设置创建人
        card.setCreator(SecurityFrameworkUtils.getLoginUserId().toString());
        cardMapper.insert(card);
        // 返回
        return card.getId();
    }

    @Override
    public void bindCard(CardUpdateVO updateReqVO) {
        CardDO cardDO = cardMapper.selectOne("card_number", updateReqVO.getCardNumber());
        if (cardDO == null) {
            throw exception(CARD_NOT_EXISTS);
        }
        if (UseStatusEnum.OVER.getStatus().equals(cardDO.getUseStatus())) {
            throw exception(CARD_USESTATUS_OVER);   //已到期
        }
        if (UseStatusEnum.USED.getStatus().equals(cardDO.getUseStatus())) {   //已经使用
            if (!cardDO.getCardDeviceNumber().equals(updateReqVO.getCardDeviceNumber())) {  //效验设备ID
                throw exception(CARD_DEVICEID_NOT);
            }
            if (cardDO.getClosure().equals(ClosureEnum.CLOSURE)) {   //已经封停
                throw exception(CARD_CLOSURE_SUSPENDED);
            }
            if (cardDO.getEndDate() < new Date().getTime()) {    //超过结束时间
                updateStatus(cardDO);  //设置已到期
                throw exception(CARD_USESTATUS_OVER);
            }
        } else {
            cardDO.setCardDeviceNumber(updateReqVO.getCardDeviceNumber());  //绑定新设备
            cardDO.setUseStatus(UseStatusEnum.USED.getStatus());//设置使用状态
            cardDO.setSellStatus(SellStatusEnum.SOLD.getStatus()); //设置销售状态
            Date startDate = new Date();
            cardDO.setStartDate(startDate.getTime());   //设置开始时间
            //设置结束时间
            switch (cardDO.getCardType()){
                case "7":
                    cardDO.setEndDate(DateUtil.offset(startDate, DateField.YEAR, 10).getTime());
                    break;
                case "6":
                    cardDO.setEndDate(DateUtil.offset(startDate, DateField.YEAR, cardDO.getCardValue()).getTime());
                    break;
                case "5":
                    cardDO.setEndDate(DateUtil.offset(startDate, DateField.MONTH, cardDO.getCardValue()).getTime());
                    break;
                case "4":
                    cardDO.setEndDate(DateUtil.offset(startDate, DateField.WEEK_OF_YEAR, cardDO.getCardValue()).getTime());
                    break;
                case "3":
                    cardDO.setEndDate(DateUtil.offset(startDate, DateField.DAY_OF_YEAR, cardDO.getCardValue()).getTime());
                    break;
                case "2":
                    cardDO.setEndDate(DateUtil.offset(startDate, DateField.HOUR_OF_DAY, cardDO.getCardValue()).getTime());
                    break;
                case "1":
                    cardDO.setEndDate(DateUtil.offset(startDate, DateField.MINUTE, cardDO.getCardValue()).getTime());
                    break;
            }
            cardMapper.updateById(cardDO);
        }
    }

    //更新状态
    public void updateStatus(CardDO cardDO){
            cardDO.setUseStatus(UseStatusEnum.OVER.getStatus());
            cardMapper.updateById(cardDO);    //设置已到期
    }

    @Override
    public void updateCard(CardSaveReqVO updateReqVO) {
        // 校验存在
        validateCardExists(updateReqVO.getId());
        // 更新
        CardDO updateObj = BeanUtils.toBean(updateReqVO, CardDO.class);
        cardMapper.updateById(updateObj);
    }

    @Override
    public void deleteCard(Long id) {
        // 校验存在
        validateCardExists(id);
        // 删除
        cardMapper.deleteById(id);
    }

    private void validateCardExists(Long id) {
        if (cardMapper.selectById(id) == null) {
            throw exception(CARD_NOT_EXISTS);
        }
    }

    @Override
    public CardDO getCard(Long id) {
        return cardMapper.selectById(id);
    }

    @Override
    public PageResult<CardDO> getCardPage(CardPageReqVO pageReqVO) {
        return cardMapper.selectPage(pageReqVO);
    }

}